# pip install fastapi
# pip install uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
import uvicorn

app = FastAPI()
origins = [
    "*"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.post("/vue-element-admin/user/login")
def user_login():
    data = {"code": 20000, "data": {"token": "admin-token"}}
    return data


@app.get("/vue-element-admin/user/info")
def user_info():
    data = {"code": 20000, "data": {"roles": ["admin"], "introduction": "I am a super administrator",
                                    "avatar": "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
                                    "name": "Super Admin"}}
    return data


@app.get("/vue-element-admin/transaction/list")
def transaction_list():
    data = {"code": 20000, "data": {"total": 20, "items": [
        {"order_no": "caADEdc3-78Af-B82B-33dA-b6FdEfacEeBF", "timestamp": 1461102929335, "username": "Deborah Martin",
         "price": 12635.7, "status": "success"},
        {"order_no": "bB84B16d-C52E-5A32-D5Ef-9FA36FaCdc20", "timestamp": 1461102929335, "username": "Paul Young",
         "price": 11743.78, "status": "success"}]}}
    return data

@app.post("/user/data")
def user_data():
    body = {"user": "zhangsan", "age": 15}
    data = Base(body).__dict__
    return data


class Base:
    def __init__(self, data, code=20000):
        self.code = code
        self.data = data


if __name__ == '__main__':
    uvicorn.run(app=app, port=5000)
